package cn.csust.net2.manager.client.app;

import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;
import java.util.Set;

import cn.csust.net2.manager.client.resources.Resources;
import cn.csust.net2.manager.client.ux.AuthorityEvent;
import cn.csust.net2.manager.shared.po.Authority;
import cn.csust.net2.manager.shared.po.Module;

import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.data.BaseModelData;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.TreePanelEvent;
import com.extjs.gxt.ui.client.store.TreeStore;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.layout.AccordionLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.treepanel.TreePanel;
import com.google.gwt.user.client.ui.AbstractImagePrototype;

public class AccordionPanel extends ContentPanel implements AppPanel {
	// 持有APP对象，便于权限对象的操作
	protected App app;

	public AccordionPanel(App app) {
		this.app = app;
		init();
	}

	private ModelData newItem(Authority a) {
		ModelData m = new BaseModelData();
		m.set("name", a.getDisplayName());
		m.set("authority", a);
		return m;
	}

	/**
	 * 初始化
	 */
	private void init() {
		this.setLayout(new AccordionLayout());
		setIcon(AbstractImagePrototype.create(Resources.ICONS.accordion()));
		setHeading("导航菜单");
		loadModules();
	}

	/**
	 * 根据权限加载各个模块，生成UI
	 */
	private void loadModules() {
		// 使用APP的对象的权限信息
		Set<Entry<Module, List<Authority>>> set = this.app.map.entrySet();
		Iterator<Entry<Module, List<Authority>>> it = set.iterator();
		while (it.hasNext()) {
			Entry<Module, List<Authority>> e = it.next();
			Module module = e.getKey();
			List<Authority> authoritys = e.getValue();
			ContentPanel a = (ContentPanel) this.getItemByItemId(module
					.getModuleName());
			if (a == null) {
				a = new ContentPanel();
				a.setAnimCollapse(true);
				a.setHeading(module.getDisplayName());
				a.setId(module.getModuleName());
				a.setLayout(new FitLayout());
				a.setIcon(AbstractImagePrototype.create(Resources.ICONS.foder()));
				this.add(a);
			}
			TreeStore<ModelData> store = new TreeStore<ModelData>();
			TreePanel<ModelData> tree = new TreePanel<ModelData>(store);
			tree.setDisplayProperty("name");
			tree.getStyle()
					.setLeafIcon(
							AbstractImagePrototype.create(Resources.ICONS
									.purpleApple()));
			for (Authority au : authoritys) {
				ModelData m = newItem(au);
				store.add(m, false);
			}
			tree.addListener(Events.OnClick,
					new Listener<TreePanelEvent<ModelData>>() {

						@Override
						public void handleEvent(TreePanelEvent<ModelData> be) {
							// 抛出权限组件点击事件
							AccordionPanel.this.app.eventBus
									.fireEvent(new AuthorityEvent(
											(Authority) be.getItem().get(
													"authority")));
						}

					});
			a.add(tree);
		}
	}

	@Override
	public BorderLayoutData getLayoutInfo() {
		BorderLayoutData layou = new BorderLayoutData(LayoutRegion.WEST, 200);
		layou.setSplit(true);
		layou.setCollapsible(true);
		layou.setMargins(new Margins(0, 5, 0, 0));
		return layou;
	}
}
